---
id: assemble-prompt
title: Assemble Prompt Node
sidebar_label: Assemble Prompt
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import Admonition from '@theme/Admonition';

## Overview

A [Chat Node](./chat) takes in a single Prompt input which can be a single message, or an array of messages that forms the message chain. The Assemble Prompt Node is used to assemble multiple chat messages into a single prompt. It takes multiple chat messages or strings as inputs and outputs a single prompt containing all the messages.

If a string is connected to any of the inputs, it will be converted to a User type chat message with the string as the message.

![Assemble Prompt Node Screenshot](./assets/assemble-prompt-node.png)

<Tabs
  defaultValue="inputs"
  values={[
    {label: 'Inputs', value: 'inputs'},
    {label: 'Outputs', value: 'outputs'},
    {label: 'Editor Settings', value: 'settings'},
  ]
}>

<TabItem value="inputs">

## Inputs

| Title       | Data Type                    | Description                                | Default Value | Notes                                                            |
| ----------- | ---------------------------- | ------------------------------------------ | ------------- | ---------------------------------------------------------------- |
| Message [i] | chat-message, chat-message[] | The ith chat message input to be assembled | N/A           | Dynamic number of inputs based on how many connections there are |

</TabItem>

<TabItem value="outputs">

## Outputs

| Title  | Data Type      | Description                 | Notes                                                                                |
| ------ | -------------- | --------------------------- | ------------------------------------------------------------------------------------ |
| Prompt | chat-message[] | The assembled chat messages | The output will be a single prompt resulting from assembling all valid chat messages |

</TabItem>

<TabItem value="settings">

## Editor Settings

This node does not have any specific settings.

</TabItem>

</Tabs>

### Example: Assembling Multiple Chat Messages

Let's say you have two [Prompt nodes](./prompt): one with the the AI's text "How can I assist you today?" and the user's response "Hello". If you want to assemble these two messages into a single prompt to feed into a Chat Node, you can do the following:

1. Add an Assemble Prompt Node to your graph.
2. Connect the output of the first Prompt node (AI's message) to `Message 1` of the Assemble Prompt Node.
3. Connect the output of the second Prompt node (user's response) to `Message 2` of the Assemble Prompt Node.
4. Connect the output of the Assemble Prompt Node to the `Prompt` input of a Chat Node.

### Example: Appending Chat Messages in a Loop

A common use case for the Assemble Prompt Node is to append chat messages in a loop. This can be achieved by using a Loop Controller Node in conjunction with the Assemble Prompt Node.

Here's how you can set this up:

1. Add a Loop Controller Node to your graph.
2. Add an Assemble Prompt Node to your graph.
3. Connect the `Output 1` of the Loop Controller Node to `Input 1` of the Assemble Prompt Node. This will feed the output of the loop back into the Assemble Prompt Node.
4. Connect the `Output 1` of the Assemble Prompt Node to `Input 1` of the Loop Controller Node. This will effectively create a feedback loop where the Assemble Prompt Node feeds back into itself.
5. Connect your first message to `Input 1 Default` of the Loop Controller Node. This will be the initial message that starts the loop.
6. Any additional messages that you want to append during each loop iteration can be connected to `Input 2`, `Input 3`, etc of the Assemble Prompt Node.

The Assemble Prompt Node will flatten the output, resulting in a list of the current messages. During each loop iteration, the Assemble Prompt Node will append any additional messages connected to its inputs.

This setup allows you to dynamically append chat messages in a loop, which can be useful in various scenarios such as creating a chatbot that can carry on a conversation with a user.

**Note:** If you want to control the number of loop iterations, you can do so by setting the `Max Iterations` property of the Loop Controller Node.

Here's a diagram to help visualize the setup:

![Loop Controller with Assemble Prompt Node](./assets/assemble-prompt-node-example-02.png)

This example demonstrates the power and flexibility of Rivet's node-based system, allowing you to create complex behaviors with just a few nodes.

### Handling Missing Inputs

If an input is missing or not a chat message, the Assemble Prompt Node will simply ignore it during the assembling process. This can be useful when dealing with optional inputs that may not always be present.

## Error Handling

If no valid chat message inputs are provided, the output will be an empty array.

**Q: What happens if I connect a non-chat message to the Assemble Prompt Node?**

A: The Assemble Prompt Node is designed to work with chat message inputs. If a non-chat message input is connected, the node will attempt to convert it to a chat message. In most cases, this will be a User type message with message being the data converted to a string. If this conversion isn't possible, the input will be ignored during the assembling process.

**Q: Can I connect an Array Node to the Assemble Prompt Node?**

A: Yes, you can connect an Array Node to the Assemble Prompt Node. Each element of the array will be treated as a separate chat message to be assembled (flattens the array).

**Q: How does the Assemble Prompt Node handle empty strings or null values?**

A: The Assemble Prompt Node will ignore any inputs that are null or not a chat message. Empty chat messages, however, will be included in the assembling operation.

## See Also
